﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading;
using System.IO;

namespace APEmailAlertService
{
    class MailUtility
    {
        private static readonly string SmtpHost = "mail.wspmo.com";
        //private static readonly string SmtpHost = "smtp.163.com";

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool SendMail(MailMessage message)
        {
            SendMailAsyn(message);
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ToList"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="attachments"></param>
        /// <returns></returns>
        public static bool SendMail(string ToList, string CCList, string subject, string body)
        {
            //发件人
            string From = "admin@wspmo.com";
            //string From = "boyischinese@163.com";
            //新建Mail
            MailMessage message = new MailMessage();
            message.IsBodyHtml = true;
            message.From = new MailAddress(From);
            //发送人
            foreach (string To in ToList.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
            {
                message.To.Add(To);
            }
            //抄送人
            foreach (string CC in CCList.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
            {
                message.CC.Add(CC);
            }
            //Email主题
            message.Subject = subject;
            //Email内容
            message.Body = body;
            return SendMail(message);
        }

        private static void SendMailAsyn(MailMessage message)
        {
            ThreadPool.QueueUserWorkItem(delegate(object state)
            {
                try
                {
                    SmtpClient sc = new SmtpClient(SmtpHost);
                    sc.Credentials = new NetworkCredential(message.From.ToString(), "p@ssw0rd01!");
                    sc.Send(message);
                }
                catch (Exception ex)
                {
                    StringBuilder sbd = new StringBuilder("Send email failed from: ");
                    sbd.Append(message.From.Address + " to: ");
                    foreach (MailAddress ma in message.To)
                    {
                        sbd.Append(ma.Address + ";");
                    }
                    LogMsg(ex.ToString());
                }
            });
        }

        private static void LogMsg(string logMsg)
        {
            FileStream file;

            if (!File.Exists("C:\\SendAPEmialsLogMsg.txt"))
            {
                file = new FileStream("C:\\SendAPEmialsLogMsg.txt", FileMode.Create, FileAccess.Write);
            }
            else
            {
                file = System.IO.File.Open(@"C:\SendAPEmialsLogMsg.txt", System.IO.FileMode.Append);
            }

            System.Console.Write(DateTime.Now);
            StringBuilder sb = new StringBuilder();
            sb.Append("============================================\r\n");
            sb.AppendFormat("Exception at {0}:{1}\r\n", DateTime.Now, logMsg);
            StreamWriter sw = new StreamWriter(file);
            sw.Write(sb.ToString());
            sw.Close();
            file.Close();
        }
    }
}
